#include "meshitem.h"

#include <QGraphicsSceneMouseEvent>
#include <iostream>
#include <QPaintEngine>
#include "geometrydata.h"
#include "config.h"
#include <QRgb>
#include <QGraphicsScene>
#include <QGraphicsView>

MeshItem::MeshItem(Triangle *triangle)
{
    this->triangle = triangle;

    polygon.append(QPointF(triangle->p1->x,triangle->p1->y));
    polygon.append(QPointF(triangle->p2->x,triangle->p2->y));
    polygon.append(QPointF(triangle->p3->x,triangle->p3->y));
    //Triangle.append(QPointF(triangle.p1->x,triangle.p1->y));
    select_ = false;

    updateParams();

}

void MeshItem::updateParams()
{
    polygon = QPolygonF();
    polygon.append(QPointF(triangle->p1->x,triangle->p1->y));
    polygon.append(QPointF(triangle->p2->x,triangle->p2->y));
    polygon.append(QPointF(triangle->p3->x,triangle->p3->y));
    pen = QPen(Config::meshColor());
    brush_empty = QBrush(Qt::NoBrush);
    double color_h = 0;
    double color_length = 0;

    color_length = abs(0 - 10);
    color_h = (360.0/color_length)*(triangle->avg_psi()+abs(0));

    brush_colored = QBrush(QColor::fromHsvF(color_h/360.0,1,1));

    pen.setWidth(0);
}

QRectF MeshItem::boundingRect() const
{
    return polygon.boundingRect();
}

QPainterPath MeshItem::shape() const
{
    QPainterPath path;
    path.addPolygon(polygon);
    return path;
}

void MeshItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    updateParams();
    if (!select_)
        painter->setBrush(brush_empty);
    else
        painter->setBrush(brush_colored);

    if (Config::drawColoredMesh())
    {
        painter->setBrush(brush_colored);
        painter->setPen(QPen(Qt::NoPen));
        painter->drawPolygon(polygon);
    }

    if (Config::drawMesh())
    for (int i = 0; i<  triangle->edges.size(); i++)
    {
        Edge *e = triangle->edges[i];
        switch (e->type)
        {
            case Edge::NONE:
                pen = QPen(Qt::blue,0);
                break;

            case Edge::IN:
                pen = QPen(Qt::green,0);
                break;

            case Edge::OUT:
                pen = QPen(Qt::yellow,0);
                break;

            case Edge::WALL:
                pen = QPen(Qt::red,0);
                break;
        }
        painter->setPen(pen);
        painter->drawLine(QPointF(e->p1->x,e->p1->y),QPointF(e->p2->x,e->p2->y));
    }

    if (Config::drawInfoText())
    {   QPointF p1 = polygon[0];
        QPointF p2 = polygon[1];
        QPointF p3 = polygon[2];
        QPointF center(( p1.x() + p2.x() + p3.x() )/3.0,( p1.y() + p2.y() + p3.y() )/3.0);
        center = this->scene()->views()[0]->mapFromScene(center);
        painter->resetTransform();
        painter->setPen(QPen(Qt::black));
        painter->setFont(QFont("Arial",10));
        painter->drawText(center,QString::number(triangle->avg_psi()));
    }
    if (select_)
    {
        QPointF p1 = polygon[0];
        QPointF p2 = polygon[1];
        QPointF p3 = polygon[2];



        QPointF tp1(p2.x()-(p2.x()-p3.x())/2.0,p2.y()-(p2.y()-p3.y())/2.0);
        QPointF tp2(p1.x()-(p1.x()-p3.x())/2.0,p1.y()-(p1.y()-p3.y())/2.0);
        QPointF tp3(p1.x()-(p1.x()-p2.x())/2.0,p1.y()-(p1.y()-p2.y())/2.0);
        QPointF center(( p1.x() + p2.x() + p3.x() )/3.0,( p1.y() + p2.y() + p3.y() )/3.0);

        p1 = QPointF(p1.x() - (p1.x()-tp1.x())/5.0, p1.y() - (p1.y()-tp1.y())/5.0);
        p2 = QPointF(p2.x() - (p2.x()-tp2.x())/5.0, p2.y() - (p2.y()-tp2.y())/5.0);
        p3 = QPointF(p3.x() - (p3.x()-tp3.x())/5.0, p3.y() - (p3.y()-tp3.y())/5.0);

        p1 =  this->scene()->views()[0]->mapFromScene(p1);
        p2 =  this->scene()->views()[0]->mapFromScene(p2);
        p3 =  this->scene()->views()[0]->mapFromScene(p3);
        center = this->scene()->views()[0]->mapFromScene(center);
        painter->resetTransform();
        painter->setPen(QPen(Qt::black));
        painter->setFont(QFont("Arial",10));
        QString info;
        info = "Psi: " + QString::number(trg()->p1->psi)+"\n"+"Omg: " + QString::number(trg()->p1->omega) + "V: " + QString::number(trg()->p1->v.x)+" "+QString::number(trg()->p1->v.y);
        painter->drawText(p1,info);
        info = "Psi: " + QString::number(trg()->p2->psi)+"\n"+"Omg: " + QString::number(trg()->p2->omega) + "V: " + QString::number(trg()->p2->v.x)+" "+QString::number(trg()->p2->v.y);
        painter->drawText(p2,info);
        info = "Psi: " + QString::number(trg()->p3->psi)+"\n"+"Omg: " + QString::number(trg()->p3->omega) + "V: " + QString::number(trg()->p3->v.x)+" "+QString::number(trg()->p3->v.y);
        painter->drawText(p3,info);


    }




}

void MeshItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mousePressEvent(event);
    update();
}

void MeshItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseMoveEvent(event);
}

void MeshItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseReleaseEvent(event);
}
